<?php
    class farm{
        private $farmID;

        public function __construct() {
            $args = func_get_args();
            if(count($args)==1){
                if(is_array($args[0])){
                    $this->addFarm($args[0]);
                }else{
                    $this->setFarmID($args[0]);
                }
            }
        }

        function setFarmID($farmID){
            $this->farmID=$farmID;
        }
        private function getFarmID(){
            return $this->farmID;
        }
        function addFarm($farmInput){
            $mysqli=  connect();

            $query="
                insert into member_farm
                    (
                        memID,
                        farmName,
                        area,
                        address,
                        subDistrictID,
                        districtID,
                        provinceID,
                        geoLocation
                    ) values (
                        '".$farmInput["memID"]."',
                        '".$farmInput["farmName"]."',
                        '".$farmInput["area"]."',
                        '".$farmInput["address"]."',
                        '".$farmInput["subDistrictID"]."',
                        '".$farmInput["districtID"]."',
                        '".$farmInput["provinceID"]."',
                        '".$farmInput["geoLocation"]."'
                    )
                ";
            $result=$mysqli->query($query);

            $this->farmID=$mysqli->insert_id;
        }
        function deleteFarm(){
            $mysqli=  connect();
            $query="
                delete from member_farm
                where       farmID='".$this->farmID."'
                ";
            return $mysqli->query($query);
        }
        function getFarm(){
            $mysqli=  connect();
            $query="
                select  *
                from    member_farm
                where   farmID='".$this->farmID."'
                ";
            $result=  $mysqli->query($query);
            $row=  $result->fetch_array(MYSQLI_ASSOC);
            return $row;
        }
        function updateFarm($input){
            $mysqli=  connect();
            $query="
                update  member_farm
                set     farmName='".$input["farmName"]."',
                        area='".$input["area"]."',
                        address='".$input["address"]."',
                        subDistrictID='".$input["subDistrictID"]."',
                        districtID='".$input["districtID"]."',
                        provinceID='".$input["provinceID"]."',
                        geoLocation='".$input["geoLocation"]."'
                where   farmID='".$input["farmID"]."'
                ";
            $result=  $mysqli->query($query);

            $this->farmID=$input["farmID"];
            return $this->getFarm();
        }
        function getFarmAddress(){
            $farm=$this->getFarm();
            $addressClass=new address();
            $subDistrict=$addressClass->getSubDistrict($farm["subDistrictID"]);
            $district=$addressClass->getDistrict($farm["districtID"]);
            $province=$addressClass->getProvince($farm["provinceID"]);

            return $farm["address"]."  ตำบล ".$subDistrict["subDistrictName"]."  อำเภอ ".$district["districtName"]."  จังหวัด ".$province["provinceName"];
        }
        function getFarmShortAddress(){
            $farm=$this->getFarm();
            $addressClass=new address();
            $subDistrict=$addressClass->getSubDistrict($farm["subDistrictID"]);
            $district=$addressClass->getDistrict($farm["districtID"]);
            $province=$addressClass->getProvince($farm["provinceID"]);

            return $farm["address"]."  ".$subDistrict["subDistrictName"]."  ".$district["districtName"]."  ".$province["provinceName"];
        }
        function addCrop($startDate,$endDate){
            $mysqli=  connect();
            $query="
                insert into member_farm_crop
                    (
                        farmID,
                        startDate,
                        endDate
                    ) values (
                        '".$this->farmID."',
                        '".$startDate."',
                        '".$endDate."'
                    )
                ";
            $result=$mysqli->query($query);
        }
    }